package cn.itcast.controller;

import cn.itcast.dao.AreaDao;
import cn.itcast.domain.Area;
import cn.itcast.service.AreaService;
import cn.itcast.utils.PinYin4jUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AreaController {
    @Autowired
    private AreaService areaService;

    /**
     *分页查询
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/area_pageQuery")
    @ResponseBody
    public Map pageQuery( Area area,int page,int rows){
        Page<Area> query = areaService.pageQuery(area,page, rows);

        Map<String,Object> result = new HashMap<>();
        result.put("total",query.getTotalElements());
        result.put("rows",query.getContent());

        return result;
    }

    /**
     * 解析文件
     * @param file
     * @return
     */
    @RequestMapping("/area_batchImport")
    @ResponseBody
    public String excelImport(MultipartFile file){
        List<Area> areas = new ArrayList<Area>();
        try {
            //加载Excel文件对象
            HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
            //默认读取第一个 sheet
            HSSFSheet sheet = workbook.getSheetAt(0);
            //遍历读取sheet中每一行
            for (Row row : sheet) {
                //一行数据 对应一个区域的对象
                if(row.getRowNum()==0){
                    //跳过
                    continue;
                }
                //跳过空行
                if(row.getCell(0)==null || StringUtils.isBlank(row.getCell(0).getStringCellValue())){
                    continue;
                }
                Area area = new Area();
                area.setId(row.getCell(0).getStringCellValue());
                area.setProvince(row.getCell(1).getStringCellValue());
                area.setCity(row.getCell(2).getStringCellValue());
                area.setDistrict(row.getCell(3).getStringCellValue());
                area.setPostcode(row.getCell(4).getStringCellValue());

                areas.add(getArea(area));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //调用业务层
        areaService.saveAll(areas);
        return "";
    }
    /**
     * 将excel文件中的数据封装到实体类中
     * @param area
     * @return
     */
    public Area getArea(Area area){
        String province = area.getProvince();
        String city = area.getCity();
        String district = area.getDistrict();
        String shortCode = getShortCode(province.substring(0,province.length()-1)
                + city.substring(0,city.length()-1)
                + district.substring(0,district.length()-1));
        String cityCode = getCityCode(city.substring(0,city.length()-1));
        area.setShortcode(shortCode);
        area.setCitycode(cityCode);

        return area;
    }

    /**
     * 将省市区名称转换成大写的拼音字符串
     * @param src
     * @return
     */
    public  String getShortCode(String src){
        String[] headByString = PinYin4jUtils.getHeadByString(src);
        return StringUtils.join(headByString);
    }

    /**
     * 城市名称转换成小写的拼音字符串
     * @param src
     * @return
     */
    public String getCityCode(String src){
        return  StringUtils.join(PinYin4jUtils.stringToPinyin(src));
    }

}